package cc.lj.internal.coupons.interfaces.facade;
import cc.lj.internal.api.coupons.dto.command.BindTicketPackageAPICommand;
import cc.lj.internal.api.coupons.dto.command.SendPackageAPICommand;
import cc.lj.internal.core.exception.SysExceptionEnum;
import cc.lj.internal.core.http.HttpResult;
import cc.lj.internal.coupons.application.service.TicketPackageAppService;
import cc.lj.internal.coupons.interfaces.dto.TicketBatchDTO;
import cc.lj.internal.coupons.interfaces.dto.TicketPackageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * 券包 api
 * @author liangxifeng
 * @date 2023/8/24 12:14
 */
@RestController
@Slf4j
@Validated
@RequestMapping("ticketPackage")
public class TicketPackageController {
    @Autowired
    private TicketPackageAppService packageAppService;

    /**
     * 通过主键查询券包
     * @return 券包DTO
     */
    @GetMapping("/getById")
    public HttpResult<TicketPackageDTO> getByid(@Min(value = 1,message = "主键必须是大于1的数字") @RequestParam Integer id) {
        TicketPackageDTO ticketPackageDTO = packageAppService.getById(id);
        return HttpResult.success(SysExceptionEnum.SUCCESS.getCode(),SysExceptionEnum.SUCCESS.getMessage(),ticketPackageDTO);
    }

    /**
     * 修改券包关联活动状态
     */
    @PostMapping("/updateBind")
    public HttpResult<Integer> updateBind(@Valid  @RequestBody BindTicketPackageAPICommand packageCommand) {
        Integer res = packageAppService.updateBind(packageCommand);
        log.info("修改券包关联活动状态api 入参："+packageCommand);
        return HttpResult.success(SysExceptionEnum.SUCCESS.getCode(),SysExceptionEnum.SUCCESS.getMessage(),res);
    }

    /**
     * 发券包接口
     * @param sendPackageAPICommand
     * @return List<券使用码>
     */
    @PostMapping("/sendPackage")
    public HttpResult<List<String>> sendPackage(@Valid @RequestBody SendPackageAPICommand sendPackageAPICommand) {
        List<String> useCodeList = packageAppService.sendPackage(sendPackageAPICommand);
        return HttpResult.success(SysExceptionEnum.SUCCESS.getCode(),SysExceptionEnum.SUCCESS.getMessage(),useCodeList);
    }

    /**
     * 获取券包列表
     * @param
     * @retrun List<TicketPackageDTO>
     */
    @PostMapping("/activityTicketPackage")
    public List<TicketPackageDTO> getList()
    {
        List<TicketPackageDTO> list = packageAppService.getList();
        return list;
    }

    /**
     * 获取券包对应的券批次列表
     * @param id
     * @return
     */
    @GetMapping("/getPackageBatchList")
    public List<TicketBatchDTO> getPackageBatchList(@Min(1) @RequestParam Long id)
    {
        List<TicketBatchDTO> list = packageAppService.getBatchList(Math.toIntExact(id));
        return list;
    }
}
